package com.yanqu.road.server.manager.banquet.baifubanquet;

import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.banquet.baifubanquet.GoodBanquet;
import com.yanqu.road.entity.banquet.baifubanquet.GoodBanquetData;
import com.yanqu.road.server.manager.banquet.CrossBanquetConfigMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Description
 * @Author cwq
 * @Data 2021/11/9 14:53
 */
public class CrossGoodBanquetActivity {

    private int activityId;

    /**
     * 所有宴会  k:宴会id v
     */
    private Map<Long, GoodBanquet> banquetMap = new ConcurrentHashMap<>();

    /**
     * 玩家的宴会集合 k:userId
     */
    private Map<Long, List<GoodBanquet>> userBanquetMap = new ConcurrentHashMap<>();

    /**
     * 正在开启的宴会 按id排序 小到大 因为目前只有一个类型，所以也表示 时间小到大
     */
    private List<GoodBanquet> banquetList = new CopyOnWriteArrayList<>();

    /**
     * 玩家使用一键赴宴 赴宴到的最大宴会id
     */
    private Map<Long, Long> maxJoinBanquetIdMap = new ConcurrentHashMap<>();

    public CrossGoodBanquetActivity(int activityId) {
        this.activityId = activityId;
    }

    /**
     * 开百福宴
     */
    public GoodBanquetData openGoodBanquet(int activityId, long userId, int type){
        BanquetInfo banquetInfo = CrossBanquetConfigMgr.getBanquetInfo(type);
        if(banquetInfo == null){
            return null;
        }
        GoodBanquetData goodBanquetData = new GoodBanquetData();
        goodBanquetData.setId(CrossGoodBanquetMgr.getMaxGoodBanquetId().incrementAndGet());
        goodBanquetData.setActivityId(activityId);
        goodBanquetData.setUserId(userId);
        goodBanquetData.setType(type);
        goodBanquetData.setCreateTime(DateHelper.getCurrentSecond());
        goodBanquetData.setEndTime(goodBanquetData.getCreateTime() + banquetInfo.getBanquetTime());
        goodBanquetData.setInsertOption();
        GoodBanquet goodBanquet = new GoodBanquet(goodBanquetData);

        addOpenGoodBanquet(goodBanquet);
        banquetMap.put(goodBanquet.getBanquetData().getId(),goodBanquet);

        return goodBanquetData;
    }

    /**
     * 加入宴会
     * @param goodBanquet
     */
    private void addOpenGoodBanquet(GoodBanquet goodBanquet){
        getBanquetList().add(goodBanquet);
        List<GoodBanquet> list = getUserBanquetList(goodBanquet.getBanquetData().getUserId(),true);
        list.add(goodBanquet);
    }

    public List<GoodBanquet> getUserBanquetList(long userId,boolean newIfNull) {
        List<GoodBanquet> list = userBanquetMap.get(userId);
        if(list == null && newIfNull){
            synchronized (userBanquetMap){
                list = userBanquetMap.get(userId);
                if(list == null){
                    list = new CopyOnWriteArrayList<>();
                    userBanquetMap.put(userId,list);
                }
            }
        }
        return list;
    }

    public Map<Long, GoodBanquet> getBanquetMap() {
        return banquetMap;
    }

    public List<GoodBanquet> getBanquetList() {
        return banquetList;
    }

    public int getActivityId() {
        return activityId;
    }

    public Map<Long, Long> getMaxJoinBanquetIdMap() {
        return maxJoinBanquetIdMap;
    }
}
